package com.niannian.service;

import java.util.List;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.IBinder;

import com.niannian.activity.HomeActivity;
import com.niannian.util.UpdateManager;
import com.niannian.R;

public class NotificationService extends Service {
	protected MessageBroadCastReceiver msgreceiver;
	private static Context mContext;

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		mContext = this;
		msgreceiver = new MessageBroadCastReceiver();
		registerReceiver(msgreceiver, new IntentFilter(UpdateManager.MESSAGE_RECEIVED_ACTION));
		super.onCreate();
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		unregisterReceiver(msgreceiver);
		super.onDestroy();
	}

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}

	class MessageBroadCastReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context arg0, Intent intent) {
			// TODO Auto-generated method stub
			String action = intent.getAction();
			if (action.equals(UpdateManager.MESSAGE_RECEIVED_ACTION)) {
				// isRuning();
				if (!isTopActivity()) {
					String msg = intent.getStringExtra("msg");
					String fromuname = intent.getStringExtra("fromuname");
					String totle = fromuname + ":" + msg;
					if (msg != null && fromuname != null) {
						showNotification(arg0, totle);
					}

				}

			}

		}

	}

	public void showNotification(Context context, String content) {
		// TODO Auto-generated method stub
		NotificationManager notifimanager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
		Notification notification = new Notification(R.drawable.ic_launcher, content, System.currentTimeMillis());
		notification.flags |= Notification.FLAG_ONGOING_EVENT;
		notification.flags |= Notification.FLAG_NO_CLEAR; // 表明在点击了通知栏中的"清除通知"后，此通知不清除，经常与FLAG_ONGOING_EVENT一起使用
		notification.flags |= Notification.FLAG_SHOW_LIGHTS;
		notification.defaults = Notification.DEFAULT_LIGHTS;
		notification.ledARGB = Color.BLUE;
		notification.ledOnMS = 5000; // 闪光时间，毫秒
		CharSequence contentTitle = "咚咚"; // 通知栏标题
		CharSequence contentText = content; // 通知栏内容
		Intent notificationIntent = new Intent(context.getApplicationContext(), HomeActivity.class);// 点击该通知后要跳转的Activity
		notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
		PendingIntent contentItent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
		notification.setLatestEventInfo(this, contentTitle, contentText, contentItent);
		notifimanager.notify(0, notification);

	}

	public static boolean isTopActivity() {
		String packageName = "com.niannian";
		ActivityManager activityManager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
		List<RunningTaskInfo> tasksInfo = activityManager.getRunningTasks(1);
		if (tasksInfo.size() > 0) {
			// 应用程序位于堆栈的顶层
			if (packageName.equals(tasksInfo.get(0).topActivity.getPackageName())) {

				return true;
			}
		}
		return false;
	}

}
